package com.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class TestXmlMain {

	public static void main(String[] args) {

//		String date = "2015-10-25";
//		
//		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//		try {
//			Date d = df.parse(date);
//			
//			Calendar cal = Calendar.getInstance();
//			cal.setTime(d);
//			
//			System.out.println(cal.get(Calendar.DAY_OF_WEEK) - 1);
//			
//			
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		String filePath = "E:\\infosys\\11_spm\\sample.xls";
		
		HSSFWorkbook hssfWorkbook = null;
		try {

			File file = new File(filePath);
			FileInputStream in = new FileInputStream(file);
			POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
			hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		HSSFSheet sheet1 = hssfWorkbook.getSheet("template");
		if (sheet1 != null){
			hssfWorkbook.removeSheetAt(0);
			try {
				FileOutputStream out = new FileOutputStream(filePath);
				hssfWorkbook.write(out);
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			System.out.println("remove sheet template ok.");
			
			try {

				File file = new File(filePath);
				FileInputStream in = new FileInputStream(file);
				POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in);
				hssfWorkbook = new HSSFWorkbook(poifsFileSystem);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		
		HSSFCellStyle style = hssfWorkbook.createCellStyle();
		HSSFSheet sheet = hssfWorkbook.createSheet("template");
		sheet.setDefaultColumnWidth(20);
		sheet.setDefaultRowHeightInPoints((float) 59.25);
		HSSFRow row = sheet.createRow(0);

		HSSFCell cell = row.createCell(0);
		//style.setFillBackgroundColor(HSSFColor.AQUA.index);
		style.setFillForegroundColor(HSSFColor.AQUA.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		cell.setCellStyle(style);
		cell.setCellValue("星期日");
		
		try {
			FileOutputStream out = new FileOutputStream(filePath);
			hssfWorkbook.write(out);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("create sheet template ok.");
	}

}
